package com.mproject.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;

import com.mproject.network.IVLE_API_Helper;
import com.mproject.network.NetworkManager;
import com.mproject.network.ProgressBarManager;

public class LoginActivity extends Activity implements OnClickListener{

	private TextView username;
	private TextView password;
	private Button login;
	private String IVLE_Token = "error";
	private boolean internet = true;

	public static ProgressDialog dialog;
	public static AlertDialog alert;
	public static SharedPreferences mypref;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout); 

		username = (TextView) findViewById(R.id.username_login);
		password = (TextView) findViewById(R.id.password_login);
		username.setText("u0906950");
		

		login = (Button) findViewById(R.id.login);
		login.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == login) {
			InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
			imm.hideSoftInputFromWindow(password.getWindowToken(), 0);


			ProgressBarManager manager=new ProgressBarManager(this)
			{

				protected void onPreExecute() {

					Log.v("manager"," in pre execute");

					dialog.setMessage("Logging in please wait...");
					dialog.setIndeterminate(true);

					// dialog.setTitle("Manager");
					dialog.show();
				}

				protected Boolean doInBackground(String... params) 
				{
					try {
						internet = true;
						IVLE_Token =  IVLE_API_Helper.loginToIVLE(username.getText().toString(), password.getText().toString());
						if (IVLE_Token.indexOf("Login fail") != -1)
							return true;
						String user_name = IVLE_API_Helper.getUserFullName(IVLE_Token);
						String user_id = IVLE_API_Helper.getUserId(IVLE_Token);
						mypref=getSharedPreferences("My_Prefs",Activity.MODE_WORLD_WRITEABLE);
						SharedPreferences.Editor editor=mypref.edit();
						editor.putString("user_name",user_name);
						editor.putString("user_id",user_id);
						editor.commit();
						
						//uncomment when server ready
						NetworkManager.updateUserInfo(user_id,user_name,mypref.getString("regid", ""));

						
						return false;
					}
					catch (Exception e) {
						internet = false;
						return true;
					}


				}
				protected void onPostExecute(Boolean result) {
					Log.v("manager"," in post execute");
					this.getDialog().dismiss();
					if (internet == false) {
						AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
						builder.setMessage("No internet connection")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}

						});
						AlertDialog alert = builder.create();
						alert.show();
					}
					else {
						if (username.getText().toString().equals("") || password.getText().toString().equals(""))
						{
							AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
							builder.setMessage("Username or Password cannot be empty")
							.setNegativeButton("OK", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									dialog.cancel();
								}

							});
							AlertDialog alert = builder.create();
							alert.show();
						}
						else {
							if(result)
							{
								AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
								builder.setMessage("Wrong Username or Password")
								.setNegativeButton("OK", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int id) {
										dialog.cancel();
									}

								});
								AlertDialog alert = builder.create();
								alert.show();
							}
							else {
								mypref=getSharedPreferences("My_Prefs",Activity.MODE_WORLD_WRITEABLE);
								SharedPreferences.Editor editor=mypref.edit();
								editor.putString("IVLE_Token",IVLE_Token);
								editor.commit();

								//process to Dashboard
								Intent intent = new Intent(LoginActivity.this, Dashboard_TabView.class);
								startActivity(intent);
								finish();

							}
						}
					}
				}

			};
			manager.execute();



		}

	}



}
